package com.bosicloud.cmb.ibp.aws.adapter.controller;

import com.amazonaws.services.cloudformation.model.Output;
import com.bosicloud.cmb.ibp.aws.adapter.commom.constant.UrlDefine;
import com.bosicloud.cmb.ibp.aws.adapter.commom.dto.CreateAwsEnvDTO;
import com.bosicloud.cmb.ibp.aws.adapter.commom.dto.CreateCFStackResponseDTO;
import com.bosicloud.cmb.ibp.aws.adapter.commom.dto.RequestDescribeStackDTO;
import com.bosicloud.cmb.ibp.aws.adapter.service.CloudFormationService;
import com.bosicloud.cmb.ibp.commom.utils.RandomString;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Jack Peng / ziliang.peng@bosicloud.com
 */
@Slf4j
@RestController
@Api(value = "AmazonCloudFormation", description = "AmazonCloudFormation接口")
public class CloudFormationController {

    @Autowired
    CloudFormationService cloudFormationService;

    @ApiOperation(value = "创建基础环境")
    @PostMapping(value = UrlDefine.CF_CREATE)
    public CreateCFStackResponseDTO createCFStack(@RequestBody @Valid CreateAwsEnvDTO awsenv) {

        return cloudFormationService.createCFStack(awsenv);
    }

    @ApiOperation(value = "获取创建基础环境Token")
    @PostMapping(value = UrlDefine.CF_CREATE_TOKEN)
    public String getCreateCFStackToken() {

        return String.format("IBP-Create-Stack-%s", RandomString.next(32));
    }

    @ApiOperation(value = "获取环境信息")
    @PostMapping(value = UrlDefine.CF_DESCRIBE_STACK)
    public List<Output> descirbeStack(@RequestBody @Valid RequestDescribeStackDTO requestDescribeStackDTO) {

        return cloudFormationService.descibeStack(requestDescribeStackDTO);
    }

}
